package com.bookmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bookmall.common.constant.PageSizeConstants;
import com.bookmall.common.entity.to.MemberInfoTo;
import com.bookmall.product.domain.BookInfoMessage;
import com.bookmall.product.domain.vo.BookInfoMessageVo;
import com.bookmall.product.domain.vo.BookMessageVo;
import com.bookmall.product.interceptor.ProductInterceptor;
import com.bookmall.product.mapper.BookInfoMessageMapper;
import com.bookmall.product.service.IBookInfoMessageService;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author 胡菜鸡
 * @Create 2022-04-08-15:38
 */
@Service
public class BookInfoMessageServiceImpl extends ServiceImpl<BookInfoMessageMapper, BookInfoMessage> implements IBookInfoMessageService {
    /**
     * 添加评论
     * @author 胡菜鸡
     * @date 2022/5/3 17:09
     */
    @Override
    public Boolean addComment(BookInfoMessage bookMessage) {
        MemberInfoTo memberInfoTo = ProductInterceptor.threadLocal.get();
        BookInfoMessage message = new BookInfoMessage();
        message.setParent(bookMessage.getParent());
        message.setBookId(bookMessage.getBookId());
        message.setUid(memberInfoTo.getId());
        message.setNickname(memberInfoTo.getNickname());
        message.setContent(bookMessage.getContent());
        message.setMessageTime(DateUtils.getNowDate());
        message.setLikesCount(0L);
        message.setReplyCount(0L);
        message.setUserIcon(memberInfoTo.getIcon());
        message.setPermission(0);
        message.setMessageType(bookMessage.getMessageType());

        return this.save(message);
    }

    /**
     * 根据书本id查询该书本的评论
     * @author 胡菜鸡
     * @date 2022/5/3 17:33
     */
    @Override
    public BookMessageVo selectBookMessageByBookId(String bookId) {
        LambdaQueryWrapper<BookInfoMessage> wrapper = new LambdaQueryWrapper<>();
        Page<BookInfoMessage> page = this.page(new Page<>(1, PageSizeConstants.SMALL),
                wrapper.eq(BookInfoMessage::getBookId, bookId));

        List<BookInfoMessage> messageList = page.getRecords();
        if(messageList == null){
            return new BookMessageVo();
        }
        List<BookInfoMessageVo> bookInfoMessageVos = messageList.stream().map(message -> {
            BookInfoMessageVo bookInfoMessageVo = new BookInfoMessageVo();
            BeanUtils.copyProperties(message, bookInfoMessageVo);
            SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = simple.format(message.getMessageTime());
            bookInfoMessageVo.setMessageTimeChina(format);
            return bookInfoMessageVo;
        }).collect(Collectors.toList());
        BookMessageVo bookMessageVo = new BookMessageVo();
        bookMessageVo.setMessages(bookInfoMessageVos);
        bookMessageVo.setPageNum(1L);
        bookMessageVo.setPageSize(page.getSize());
        bookMessageVo.setTotalCount(page.getTotal());
        bookMessageVo.setTotalPage(page.getPages());
        return bookMessageVo;
    }
}
